package com.web.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.web.common.Sys;
import com.web.model.PageBean;
import com.web.model.TCathectic;
import com.web.model.TUser;
import com.web.model.TUserBalanceRecord;
import com.web.model.TWinNum;
import com.web.model.TWinResult;
import com.web.model.constant.Constant;
import com.web.service.TCathecticService;
import com.web.service.TUserBalanceRecordService;
import com.web.service.TUserService;
import com.web.service.TWinNumService;
import com.web.service.TWinResultService;
import com.web.service.TWinTypeService;

@Component("winNumUtil")
public class WinNumUtil {
	
	private static Logger logger = Logger.getLogger(WinNumUtil.class);
	
	@Autowired
	private TUserService userService;

	@Autowired
	private TWinNumService winNumService;
	
	@Autowired
	private TWinTypeService winTypeService;
	
	@Autowired 
	private TWinResultService winResultService;
	
	@Autowired
	private TCathecticService cathecticService;
	
	@Autowired
	private TUserBalanceRecordService userBalanceRecordService;
	
	@Resource(name="sys")
	private Sys sys;

	private TWinNum insertWinNum = new TWinNum();
	private String[] numArr;
	private Long termcd;
	private Map<String, Object> param = new HashMap<String, Object>();
	
	private List<TCathectic> cathecticList;
	private TWinResult insertWinResult = new TWinResult(); 
	private TUserBalanceRecord userBalanceRecord = new TUserBalanceRecord();
	private List<TWinResult> winResultList;
	private TUser user;
	private TUser up ;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	PageBean<TWinNum> winNumPage;
	
	public void checkWinNum(TWinNum winNum,String[] numArr){
		this.insertWinNum = winNum;
		this.numArr = numArr;
		this.termcd = winNum.getTermCd();
		processResult();//计算开奖结果
		prossFirstAndSecond();//计算冠亚和
		// 同时处理订单消息
		processCathectic(termcd);
	}
	
	/**
	 * 处理开奖结果
	 */
	private void processResult(){
		param.clear();
		param.put("winTypeWay", 1);
		param.put("sort", "win_type_code");
		
		insertWinResult.setTermCd(insertWinNum.getTermCd() + "");
		for(int i = 0;i < numArr.length ;i++){ //处理1-10名
			insertWinResult.setDictType(checkDictType(i));
			
			insertWinResult.setDictCode(numArr[i]);
			winResultService.insert(insertWinResult);//号码
			
			insertWinResult.setDictCode(checkBigOrSmall(numArr[i]));
			winResultService.insert(insertWinResult);//大小
			
			insertWinResult.setDictCode(checkDoubleOrSingle(numArr[i]));
			winResultService.insert(insertWinResult);//单双
			if(i < 5){
				insertWinResult.setDictCode(checkDragonOrTiger(numArr[i], numArr[numArr.length - i - 1]));
				winResultService.insert(insertWinResult);//龙虎
			}
		}
	}
	
	private void prossFirstAndSecond(){
		int firstAndSecond = (Integer.parseInt(numArr[0]) + Integer.parseInt(numArr[1]));//冠亚和值
		insertWinResult.setDictType("11");
		insertWinResult.setDictCode(firstAndSecond + "");
		winResultService.insert(insertWinResult);//冠亚和
		
		int bigOrSmall = 21; //小
		if(firstAndSecond > 11){//大
			bigOrSmall = 20;
		}
		insertWinResult.setDictCode(bigOrSmall + "");
		winResultService.insert(insertWinResult);//冠亚和大小
		
		int doubleOrSingle = 22;//单
		if(firstAndSecond % 2 == 0){
			doubleOrSingle = 23;
		}
		insertWinResult.setDictCode(doubleOrSingle + "");
		winResultService.insert(insertWinResult);//冠亚和单双
		
	}

	private String checkBigOrSmall(String numStr){
		String result = "14";
		int num = Integer.parseInt(numStr);
		if(num >= 6){
			result = "13"; 
		}
		return result;
	}
	
	private String checkDoubleOrSingle(String numStr){
		String result = "11";
		int num = Integer.parseInt(numStr);
		if(num % 2 == 0){
			result = "12"; 
		}
		return result;
	}
	
	private String checkDragonOrTiger(String numStr1,String numStr2){
		String result = "16";
		int num1 = Integer.parseInt(numStr1);
		int num2 = Integer.parseInt(numStr2);
		if(num1 > num2){
			result = "15";
		}
		return result;
	}
	
	private String checkDictType(int i){
		String dictType = "";
		switch(i){
			case 0:
				dictType = "01";
				break;
			case 1:
				dictType = "02";
				break;
			case 2:
				dictType = "03";
				break;
			case 3:
				dictType = "04";
				break;
			case 4:
				dictType = "05";
				break;
			case 5:
				dictType = "06";
				break;
			case 6:
				dictType = "07";
				break;
			case 7:
				dictType = "08";
				break;
			case 8:
				dictType = "09";
				break;
			case 9:
				dictType = "10";
				break;
			default:
				dictType= "11";
				break;
		}
		return dictType;
	}
	
	private void processCathectic(Long checktermCd){
		logger.info("processCathectic start ");
		Date now = new Date();
		param.clear();
		param.put("termCd", checktermCd);
		param.put("isWin", "0");
		cathecticList = cathecticService.selectList(param); //当期订单列表
		winResultList = winResultService.selectList(param); //当期开奖结果
		for (TWinResult r : winResultList) {
			for (int i = 0;i < cathecticList.size();i++) {
				TCathectic c = cathecticList.get(i);
				if(r.getDictType().equals(c.getCathecticType()) && r.getDictCode().equals(c.getCathecticCode())){
					logger.info("type:" + r.getDictType() + "-code:"+r.getDictCode() + "--amt:"+c.getWinAmt());
					/****修改订单表状态为赢****/
					c.setIsWin("1");
					cathecticService.updateByPrimaryKey(c); //修改下单表
					/*******增加资金记录*************/
					userBalanceRecord.setBalance(c.getWinAmt());
					userBalanceRecord.setUserId(c.getCreateUser());
					userBalanceRecord.setBalanceContext(sys.getOpenBalanceContext(0, checktermCd, c.getCathecticType(), c.getCathecticCode()));
					userBalanceRecord.setBalanceDate(now);
					userBalanceRecord.setBalanceType(Constant.USER_BALANCE_TYPE_3);
					userBalanceRecordService.insert(userBalanceRecord);
					/********更改用户余额**************/
					user = userService.selectByPrimaryKey(c.getCreateUser());
					up = new TUser();
					up.setUserBalance(user.getUserBalance() + c.getWinAmt());
					up.setUserId(user.getUserId());
					userService.updateByPrimaryKey(up);
				}
			}
			/**********处理输掉的订单***************/
			cathecticService.updateLoseCathectic(termcd);
		}
		logger.info("processCathectic end");
	}

}
